import torch
import sounddevice as sd
import time
from settings import speaker_dropdown

language = 'ru'
model_id = 'ru_v3'
sample_rate = 48000
put_accent = True
put_yo = True
device = torch.device('cpu')

model, _ = torch.hub.load(repo_or_dir='snakers4/silero-models',
                          model='silero_tts',
                          language=language,
                          speaker=model_id)
model.to(device)

def va_speak(what: str, main_window):
    global speaker
    speaker = speaker_dropdown
    audio = model.apply_tts(text=what + ".",
                            speaker=speaker,
                            sample_rate=sample_rate,
                            put_accent=put_accent,
                            put_yo=put_yo)
    main_window._insert_message(what, "Ассистент")
    main_window.update()
    sd.play(audio, sample_rate * 1.05)

    time.sleep(len(audio) / sample_rate + 0.5)
    sd.stop()


